package org.lucava.boatskeeper.client.control;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import java.util.List;
import org.lucava.boatskeeper.client.event.category.CancelEditCategoryEvent;
import org.lucava.boatskeeper.client.event.category.CategoryHandler;
import org.lucava.boatskeeper.client.event.category.DeleteCategoriesEvent;
import org.lucava.boatskeeper.client.event.category.EditCategoryEvent;
import org.lucava.boatskeeper.client.event.category.ListCategoriesEvent;
import org.lucava.boatskeeper.client.event.category.SaveCategoryEvent;
import org.lucava.boatskeeper.client.service.CategoryService;
import org.lucava.boatskeeper.client.service.CategoryServiceAsync;
import org.lucava.boatskeeper.client.view.CategoryEditView;
import org.lucava.boatskeeper.client.view.CategoryListView;
import org.lucava.boatskeeper.shared.dto.CategoryDto;
import org.lucava.lib.client.control.Control;

public class CategoryController extends Control implements CategoryHandler {

    private CategoryListView categoryListView;
    private CategoryEditView categoryEditView;
    private CategoryServiceAsync categoryService = GWT.create(CategoryService.class);

    @Override
    public void onInit() {
    }

    @Override
    public void onInitHandlers() {
        getEventBus().addHandler(SaveCategoryEvent.TYPE, CategoryController.this);
        getEventBus().addHandler(EditCategoryEvent.TYPE, CategoryController.this);
        getEventBus().addHandler(CancelEditCategoryEvent.TYPE, CategoryController.this);
        getEventBus().addHandler(DeleteCategoriesEvent.TYPE, CategoryController.this);
        getEventBus().addHandler(ListCategoriesEvent.TYPE, CategoryController.this);
    }

    @Override
    public void onSaveCategory(SaveCategoryEvent event) {
        if (categoryEditView.isValid()) {
            categoryService.saveCategory(categoryEditView.getCategory(), new AsyncCallback<Boolean>() {
                @Override
                public void onSuccess(Boolean result) {
                    getEventBus().fireEvent(new ListCategoriesEvent());
                }

                @Override
                public void onFailure(Throwable caught) {
                    Window.alert("Impossible de sauvegarder la catégory \n ->" + caught.getLocalizedMessage());
                }
            });
        }
    }

    @Override
    public void onEditCategory(EditCategoryEvent event) {
        categoryEditView = new CategoryEditView(getEventBus());

        if (event.getId() != null) {
            categoryService.getCategory(event.getId(), new AsyncCallback<CategoryDto>() {
                @Override
                public void onSuccess(CategoryDto result) {
                    categoryEditView.setCategory(result);
                }

                @Override
                public void onFailure(Throwable caught) {
                    Window.alert("Impossible de modifier la catégorie \n ->" + caught.getLocalizedMessage());
                }
            });
        }

        assignView("content", categoryEditView);



    }

    @Override
    public void onCancelEditCategory(CancelEditCategoryEvent event) {
        assignView("content", categoryListView);

    }

    @Override
    public void onDeleteCategories(DeleteCategoriesEvent event) {

        categoryService.removeCategories(categoryListView.getSelectedRows(), new AsyncCallback<Boolean>() {
            @Override
            public void onSuccess(Boolean result) {
                fetchCategories();
            }

            @Override
            public void onFailure(Throwable caught) {
                Window.alert("Impossible de supprimer la catégorie \n ->" + caught.getLocalizedMessage());
            }
        });

    }

    private void fetchCategories() {
        categoryService.getCategories(new AsyncCallback<List<CategoryDto>>() {
            @Override
            public void onSuccess(List<CategoryDto> result) {
                categoryListView.setCategories(result);
            }

            @Override
            public void onFailure(Throwable caught) {
                Window.alert("Impossible de récupérer les catégories \n ->" + caught.getLocalizedMessage());
            }
        });
    }

    @Override
    public void onListCategories(ListCategoriesEvent aThis) {
        categoryListView = new CategoryListView(getEventBus());
        fetchCategories();
        assignView("content", categoryListView);
    }
}
